﻿$(document).ready(function ()
{
    $(".registerButton").click(function ()
    {
        $("#registerForm").submit(function (event)
        {
            event.preventDefault();

            var $form = $(this),
                username = $form.find('input[name="name-field"]').val(),
                userPassw = $form.find('input[name="password-field"]').val(),
                userEmail = $form.find('input[name="email-field"]').val();

            url = "/User/Register";
            $.post(url, { username: username, password: userPassw, email: userEmail }, function ()
            {
                console.log("First barrier passed!");
            });
            url = "/User/Login";
            $.post(url, { username: username, password: userPassw }, function () {
                console.log("second barrier passed!");
                $("#confirmOverlay").show();
                $(".okButton").focus();
            });
        });


    });

    $(".loginButton").click(function ()
    {
        //AJAX REQUEST
        $("#loginForm").submit(function (event)
        {
            event.preventDefault();

            var $form = $(this),
                username = $form.find('input[name = "name-field"]').val(),
                userPassw = $form.find('input[name = "password-field"]').val();

            console.log(username + " " + userPassw);
            url = "Login";

            $.post(url, { username: username, password: userPassw }, function () {
                $("#confirmOverlay").show();
                $(".okButton").focus();
            });
        });
    });

    $("#confirmButtons").click(function (e)
    {
        e.preventDefault();
        $('#confirmOverlay').fadeOut("fast");
        $('#name-field').val("");
        $('#password-field').val("");
        $('#email-field').val("");
        window.location.replace("/Home/MainMenu");
    });
});